﻿using System;
using System.Collections.Generic;
using System.Text;
using DaiChong.Lib.Http;
using System.Text.RegularExpressions;
using WTest.Model;

namespace WTest
{
    public class AsyncBaidu : AsyncBase
    {
        public string SiteName { get; set; }

        protected override void AsyncWork()
        {
            string[] mtype = new string[] { "movie", "tv", "katong", "dianshi" };
            Submit web = new Submit();
            foreach (string s in mtype)
            {
                web.URL = "http://top.baidu.com/buzz.php?p=" + s;
                web.Encode = Encoding.GetEncoding("gb2312");
                web.Get();

                string tempHTML = Regex.Match(web.Html, "相关链接</td></tr>[\\s|\\S]+?</tbody>").Groups[0].Value;
                MatchCollection mc = Regex.Matches(tempHTML, "<tr>[\\s|\\S]+?</tr>");
                foreach (Match m in mc)
                {
                    tempHTML = m.Groups[0].Value;
                    MatchCollection mac = Regex.Matches(tempHTML, "<td( class=\"key\")?>[\\s|\\S]+?</td>");
                    string fname = Regex.Match(mac[0].Groups[0].Value, "blank\">([^<]+)").Groups[1].Value;
                    string hits = Regex.Match(mac[3].Groups[0].Value, "<td>(\\d+)").Groups[1].Value;
                    Movie movie = new Movie(SiteName);
                    int mid = 0;
                    int cid = 0;
                    movie.Exist(fname, out mid, out cid);
                    movie.vod_stars = 10;
                    movie.vod_hits = int.Parse(hits);
                    movie.vod_id = mid;
                    movie.UpdateStarAndHits(movie);
                }
            }
        }
    }
}
